//
//  ShopingToolBar.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/7/28.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "ShopingToolBar.h"
#import "ShopButton.h"
#import "MBProgressHUD+MJ.h"

@interface ShopingToolBar()

@property (nonatomic,strong) NSArray *titles;
@property (nonatomic,weak) ShopButton *focus;
@property (nonatomic,weak) ShopButton *buyCar;
@property (nonatomic,weak) UIButton *addCar;
@property (nonatomic,weak) UIButton *exchangeBtn;
@end
@implementation ShopingToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 0.3)];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:lineView];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buyCarAdd:) name:BuyCarAddNotifation object:nil];
    }
    return self;
}
//动画完成后算一次有效点击
- (void)buyCarAdd:(NSNotification *)noti
{
    NSDictionary *dict = noti.userInfo;
    self.buyCar.tags += [dict[@"count"] integerValue];
}
- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    
    if (titles.count == 1) {
        NSLog(@"%ld",titles.count);
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [button setTitle:@"兑换商品" forState:(UIControlStateNormal)];
        [button setBackgroundColor:[UIColor redColor]];
        [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        button.frame = CGRectMake(0, 0, ScreenW, 50);
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = ShopingToolBarTypeExchange;
        self.exchangeBtn = button;
        [self addSubview:button];
        
    }else {
        float btnW = (ScreenW-0.7*ScreenW)/(titles.count-1);
        for (NSInteger i=0; i<titles.count; i++) {
            if (i != titles.count-1) {
                ShopButton *btn = [[ShopButton alloc]initWithFrame:CGRectMake(btnW*i,0.15, btnW, 49.8)];
                [btn setTitle:titles[i] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                btn.titleLabel.font = Font(12);
                [btn setImage:GetImage(titles[i]) forState:UIControlStateNormal];
                NSString *highImageName = [NSString stringWithFormat:@"%@_h",titles[i]];
                [btn setImage:GetImage(highImageName) forState:UIControlStateHighlighted];
                btn.tag = ShopingToolBarTypeBuyCar;
                [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
                self.buyCar = btn;
            }else{
                UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(btnW*i,0.15, 0.7*ScreenW, 49.8)];
                [btn setTitle:titles[i] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                btn.backgroundColor = [UIColor redColor];
                btn.titleLabel.font = Font(18);
                btn.tag = ShopingToolBarTypeAdd;
                [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                self.addCar = btn;
                [self addSubview:btn];
            }
        }
        
    }
}
//toolbar 按钮点击
- (void)btnClick:(UIButton *)btn
{
//    if (btn.tag == ShopingToolBarTypeFocuse) {
//        btn.selected = !btn.selected;
//    }
    if (![self.delegate respondsToSelector:@selector(shopingToolBarClick:btn:)]) return;
    [self.delegate shopingToolBarClick:(ShopingToolBarType)btn.tag btn:btn];
}


- (void)setGooodsModel:(AddGoodsModel *)gooodsModel
{
    _gooodsModel = gooodsModel;
    self.focus.selected = gooodsModel.isfollow;
    self.buyCar.tags = gooodsModel.productInShopCarCount;
}

+ (instancetype)shopToolBarWithTitles:(NSArray *)titles
{
    ShopingToolBar *toolBar = [[ShopingToolBar alloc]initWithFrame:CGRectMake(0, ScreenH-64-50, ScreenW, 50)];
    toolBar.titles = titles;
    return toolBar;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:BuyCarAddNotifation object:nil];
}

@end
